package executor

import (
	"context"
	"os/exec"
	"time"
)

type CommandJob struct {
	jobID   string
	jobName string
	Command string
	Timeout time.Duration
}

func NewCommandJob(name, cmd string, timeout time.Duration) *CommandJob {
	return &CommandJob{
		jobID:   name + "-cmd",
		jobName: name,
		Command: cmd,
		Timeout: timeout,
	}
}

func (c *CommandJob) ID() string   { return c.jobID }
func (c *CommandJob) Name() string { return c.jobName }
func (c *CommandJob) Type() string { return "command" }

func (c *CommandJob) Run(ctx context.Context) error {
	ctx, cancel := context.WithTimeout(ctx, c.Timeout)
	defer cancel()

	command := exec.CommandContext(ctx, "sh", "-c", c.Command)
	return command.Run()
}
